//使用通用定时器实现LED闪烁
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "timer.h"
#include "led.h"
int time3_num;
//timer2----timer5 4个通用定时器
//（arr+1）*(psc+1)*1/72000000秒
void timer3_init(u16 arr,u16 psc)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	/*第1步：使能定时器TIM3的时钟*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	
	
	/*第2步：初始化TIM3的相关参数*/
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//不做时钟分割
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period=arr;//自动装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler=psc;//预分频系数
	//TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
	
	/*第3步：使能定时器的更新中断*/
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
	
	/*第4步：初始化中断相关参数*/
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStruct.NVIC_IRQChannel=TIM3_IRQn;//指定中断通道为TIM3_IRQn
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;//使能TIM3_IRQn通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
	NVIC_Init(&NVIC_InitStruct);
	
	/*第5步：使能TIM3*/
	TIM_Cmd(TIM3,ENABLE);
}
/*第6步：编写定时器TIM3的中断函数*/
void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)//判断是否确实发生了定时器更新中断
	{
		LED=~LED;
		time3_num++;
	}
	TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//清除TIM3的更新中断标志
}
